//
//  QColorUtility.m
//  QoilPublic
//
//  Created by zhuang on 16/7/1.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QColorUtility.h"

@implementation QColorUtility

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert {
    
    if(stringToConvert == nil || [stringToConvert isEqualToString:@""]){
        return nil;
    }
    NSString * cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

/*
 线的颜色
 */

//线的颜色
+ (UIColor *)defaultLineColor{

    return [self colorWithHexString:@"#e1e1e1"];
}

//背景色
+ (UIColor *)defaultBackGroundColor{

    return [self colorWithHexString:@"#f7f7f7"];

}

//主色调
+ (UIColor *)defaultMainColor{

    return [self colorWithHexString:@"#3090e2"];
}

//灰色按钮
+ (UIColor *)defaultGrayButtonColor{
    
    return [self colorWithHexString:@"e1e1e1"];
}


/*
 字体颜色
 */

//黑色
+ (UIColor *)defaultBackColor{

    return [self colorWithHexString:@"#555555"];
}

//深灰
+ (UIColor *)defaultDeepGrayColor{

    return [self colorWithHexString:@"#919191"];

}

//浅灰
+ (UIColor *)defaultLowGrayColor{
    
    return [self colorWithHexString:@"#cecece"];
}

//蓝色
+ (UIColor *)defaultBlueColor{

    return [self colorWithHexString:@"#3090e2"];
}

//红色
+ (UIColor *)defaultRedColor{

    return [self colorWithHexString:@"#ff6666"];
}

//置灰
+ (UIColor *)defaultDisGrayColor{

    return [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.5];
}

+ (NSMutableAttributedString *)textWithString:(NSString *)textStr{
    NSMutableAttributedString *subSigninattString = [[NSMutableAttributedString alloc] initWithString:textStr];
    NSString *pattern = @"\\d+(\\.\\d+)?";
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray *results = [regex matchesInString:textStr options:0 range:NSMakeRange(0, textStr.length)];
    for (NSTextCheckingResult *result in results) {
        [subSigninattString addAttribute:NSForegroundColorAttributeName value:[QColorUtility defaultRedColor] range:result.range];
    }
    return subSigninattString;
}

+ (NSMutableAttributedString *)textWithString:(NSString *)textStr changeColorText:(NSString *)text{
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:textStr attributes:nil];
    [attString addAttribute:NSForegroundColorAttributeName value:[QColorUtility defaultRedColor] range:[textStr rangeOfString:text]];
    return attString;
}

//正则表达式判断纯数字
+ (BOOL)validateNumber:(NSString *) textString{
    NSString* number=@"^[0-9]+$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}

//判断是否是电话号码
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


+ (UIImage *)buttonImageFromColor:(UIColor *)color rect:(CGRect)rect{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

#pragma mark 不失真拉伸图片
+(UIImage *)stretchWithName:(NSString *)name
{
    //name为传入的要拉伸图片的图片名
    UIImage *image = [UIImage imageNamed:name];
    
    //stretchableImageWithLeftCapWidth: topCapHeight:
    //这个方法的两个参数分别是左边不拉伸的区域和上面不拉伸的区域，相当于把图片中点1*1的像素进行拉伸
    return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
}
@end
